//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package itbour.onetouchshow.utils;

import android.annotation.SuppressLint;

import com.afollestad.materialdialogs.DialogInit;
import com.afollestad.materialdialogs.OneToucDialog;

import java.util.Timer;
import java.util.TimerTask;

public class OneTouchDialogUtils extends DialogInit {

    public static OneTouchPorgressOver oneTouchPorgressOver;
    protected static ProgressTimerTask mProgressTimerTask;

    @SuppressLint("LongLogTag")
    public static synchronized void startProgressTimer(OneToucDialog dialog, OneTouchDialogUtils.OneTouchPorgressOver mOneTouchPorgressOver, int endProgress, int speed) {
        cancelProgressTimer();
        oneTouchPorgressOver = mOneTouchPorgressOver;
        dialog.downloadProgressBar.setProgress(dialog.downloadProgress);
        dialog.downloadProgressBar.setIndeterminate(false);
        dialog.downloadProgressBar.setMax(100);
        updateProcessTimer = new Timer();
        mProgressTimerTask = new ProgressTimerTask(dialog, endProgress);
        updateProcessTimer.schedule(mProgressTimerTask, 0L, (long) speed);
    }

    public static void cancelProgressTimer() {
        if (updateProcessTimer != null) {
            updateProcessTimer.cancel();
            updateProcessTimer = null;
        }

        if (mProgressTimerTask != null) {
            mProgressTimerTask.cancel();
            mProgressTimerTask = null;
        }

    }


    public interface OneTouchPorgressOver {
        void onMackOver();
    }

    private static class ProgressTimerTask extends TimerTask {
        public OneToucDialog dialog;
        public int endProgress;

        public ProgressTimerTask(OneToucDialog mDialog, int mProgress) {
            this.dialog = mDialog;
            this.endProgress = mProgress;
        }

        public synchronized void run() {
            this.dialog.getView().post(new Runnable() {
                @SuppressLint({"LongLogTag"})
                public void run() {
                    if (dialog.downloadProgress > 100) {
                        dialog.tvprogress.setText("100%");
                        cancelProgressTimer();
                        dialog.dismiss();
                        if (oneTouchPorgressOver != null) {
                            oneTouchPorgressOver.onMackOver();
                            oneTouchPorgressOver = null;
                        }
                    } else if (dialog.downloadProgress >= endProgress && endProgress < 100) {
                        dialog.tvprogress.setText(dialog.downloadProgress + "%");
                        dialog.downloadProgressBar.setProgress(dialog.downloadProgress);
                        cancelProgressTimer();
                    } else {
                        dialog.downloadProgressBar.incrementProgressBy(1);
                        dialog.tvprogress.setText(dialog.downloadProgress + "%");
                        dialog.downloadProgress++;
                    }

                }
            });
        }
    }
}
